package homework1

import homework1.DrinkDemo.{bottleCount, coverCount, totalCount}

object DrinkDemo {

  var totalCount = 5
  var coverCount = 5
  var bottleCount = 5

  def main(args: Array[String]): Unit = {

    getBeerCount(100)
    getBeerCount2(100)

  }

  def getBeerCount(money: Int): Unit = {
    totalCount = money / 2
    coverCount = totalCount
    bottleCount = totalCount
    while (coverCount >= 5 || bottleCount >= 3) {
      totalCount += getBeerCount(coverCount, bottleCount)
    }
    println(s"一共可以买${totalCount}瓶")
  }

  def getBeerCount(cover: Int, bottle: Int): Int = {
    val count = cover / 5 + bottle / 3
    coverCount = cover % 5 + count
    bottleCount = bottle % 3 + count
    return count
  }

  def getBeerCount2(money: Int): Unit = {
    var totalCount = money / 2
    val coverCount = totalCount
    val bottleCount = totalCount
    totalCount += getBeerCount2(coverCount, bottleCount)
    println(s"一共可以买${totalCount}瓶")
  }

  def getBeerCount2(cover: Int, bottle: Int): Int = {
    var count = cover / 5 + bottle / 3
    val coverCount = cover % 5 + count
    val bottleCount = bottle % 3 + count
    if (coverCount >= 5 || bottleCount >= 3) {
      count += getBeerCount2(coverCount, bottleCount)
    }
    return count
  }


}
